package com.cch.cooperation.api.support.util;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SupportFileUtil {

    private final static List<String> SUFFIX_IMG = Arrays.asList("jpg", "jpeg", "png", "gif");

    public static Map<String, Object> getParam(String path) {
        Map<String, Object> param = new HashMap<>();
        try {
            if (isImg(path)) {
                BufferedImage img = ImageIO.read(new ByteArrayInputStream(FileUtil.readBytes(path)));
                param.put("width", img.getWidth());
                param.put("height", img.getHeight());
            }
        } catch (Exception e) {
            log.error("获取文件参数失败", e);
        }
        return param;
    }

    public static boolean isImg(String path) {
        return SUFFIX_IMG.stream().anyMatch(o -> path.toLowerCase().endsWith(o));
    }
}
